#!/usr/bin/env bash
#
PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"

. "${PROJECT_DIR}/scripts/utils.sh"

action=${1-}
target=${2-}
args=("$@")

check_config_file() {
  if [[ -f .env ]]; then
    if ! ls -l .env | grep "${CONFIG_FILE}" &>/dev/null; then
      echo ".env $(gettext 'There is a problem with the soft connection, Please update it again')"
      rm -f .env
    fi
  fi

  if [[ ! -f .env ]]; then
    ln -s "${CONFIG_FILE}" .env
  fi
}

pre_check() {
  check_config_file || return 3
}

usage() {
  echo "$(gettext 'Application Deployment Management Script')"
  echo
  echo "Usage: "
  echo "  ./smsctl.sh [COMMAND] [ARGS...]"
  echo "  ./smsctl.sh --help"
  echo
  echo "Installation Commands: "
  echo "  install           $(gettext 'Install Application')"
  echo "  uninstall         $(gettext 'Uninstall Application')"
  echo "  upgrade           $(gettext 'Upgrade Application')"
  echo "  upgrade [version] $(gettext 'Upgrade Application')"
  echo
  echo "Management Commands: "
  echo "  start             $(gettext 'Start   Application')"
  echo "  stop              $(gettext 'Stop    Application')"
  echo "  close             $(gettext 'Close   Application')"
  echo "  restart           $(gettext 'Restart Application')"
  echo "  status            $(gettext 'Check   Application')"
  echo
  echo "More Commands: "
  echo "  version        $(gettext 'View Application version')"
  echo "  cmd         $(gettext 'View cmd')"
  echo "  tail [service]    $(gettext 'View log')"
  echo
}

service_to_docker_name() {
  service=$1
  if [[ "${service:0:3}" != "sm" ]]; then
    service=sm_${service}
  fi
  echo "${service}"
}

EXE=""

start() {
 # echo ${EXE}
  ${EXE} up -d
}

stop() {
  if [[ -n "${target}" ]]; then
    ${EXE} stop "${target}" && ${EXE} rm -f "${target}"
    return
  fi
  services=$(get_docker_compose_services ignore_db)
  for i in ${services}; do
    ${EXE} stop "${i}"
  done
  for i in ${services}; do
    ${EXE} rm -f "${i}" >/dev/null
  done
}




restart() {
  stop
  echo -e "\n"
  start
}
main() {
  if [[ "${action}" == "help" || "${action}" == "h" || "${action}" == "-h" || "${action}" == "--help" ]]; then
    echo ""
  else
    pre_check || return 3
    EXE=$(get_docker_compose_cmd_line)
  fi
  case "${action}" in
  install)
    bash "${PROJECT_DIR}/scripts/install.sh"
    ;;
  uninstall)
    bash "${PROJECT_DIR}/scripts/uninstall.sh"
    ;;
  upgrade)
    bash "${PROJECT_DIR}/scripts/upgrade.sh" "$target"
    ;;
  start)
    start
    ;;
  restart)
    restart
    ;;
  stop)
    stop
    ;;
  status)
    ${EXE} ps
    ;;
  cmd)
    echo "${EXE}"
    ;;
  tail) 
    if [[ -z "${target}" ]]; then
      ${EXE} logs --tail 100 -f
    else
      docker_name=$(service_to_docker_name "${target}")
      docker logs -f "${docker_name}" --tail 100
    fi
    ;;
  version)
    get_current_version
    ;;
  help)
    usage
    ;;
  --help)
    usage
    ;;
  -h)
    usage
    ;;
  *)
    echo "No such command: ${action}"
    usage
    ;;
  esac
}

main "$@"
